# 源地址

# 解码
# base = "http%3A%2F%2Fshp%2Eqpic%2Ecn%2Fishow%2F2735062915%2F1593417523%5F84828260%5F29743%5FsProdImgNo%5F8%2Ejpg%2F200"
# url = parse.unquote(base)
# print(url)

# 将后面的200变成0 可以转换成高清壁纸

# 获取图片url中的page参数 可以进行翻页


import requests
from urllib import parse
import pprint
from urllib.request import urlretrieve
import os
import threading
import queue

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36',
}



# 生产者
class Producer(threading.Thread):

    def __init__(self,page_queue,image_queue,*args,**kwargs):
        super(Producer,self).__init__(*args,**kwargs)
        self.page_queue = page_queue
        self.image_queue = image_queue


    def run(self):
        while self.page_queue.empty():
            page_url = self.page_queue.get()
            response = requests.get(page_url, headers=headers)

            json_dic = response.json()['List']
            for data in json_dic:
                new_url = get_images(data)
                image_name = parse.unquote(data.get('sProdName')).replace('1:1', '').strip()

                # 文件名
                dir_path = os.path.join('images', image_name)
                os.mkdir(dir_path)

                for index,image_url in enumerate(new_url):
                    self.image_queue.put({'image_url':image_url,'dir_path':os.path.join(dir_path,f'{index+1}.jpg')})


# 消费者
class Consumer(threading.Thread):

    def __init__(self,image_queue,*args,**kwargs):
        super(Consumer,self).__init__(*args,**kwargs)
        self.image_queue = image_queue

    def run(self):

        # 下载
        while True:
            try:
                image_obj = self.image_queue.get(timeout=10)
                image_url = image_obj.get('image_url')
                dir_path = image_obj.get('dir_path')
                urlretrieve(image_url, dir_path)
                print('下载成功%s' % (image_url))
            except:
                break

def get_images(data):
    images_urls = []
    for i in range(1, 9):
        images_url = parse.unquote(data.get(f'sProdImgNo_{i}')).replace('200', '0')
        images_urls.append(images_url)

    return images_urls


def main():
    page_queue = queue.Queue(18)
    image_queue = queue.Queue(1000)
    for i in range(0,18):
        page_url = "https://apps.game.qq.com/cgi-bin/ams/module/ishow/V1.0/query/workList_inc.cgi?" \
               "activityId=2735&sVerifyCode=ABCD&sDataType=JSON&iListNum=20&totalpage=0&page={page}" \
               "&iOrder=0&iSortNumClose=1&iAMSActivityId=51991&_everyRead=true&iTypeId=2&iFlowId=267733&" \
                   "iActId=2735&iModuleId=2735&_=1593425574105".format(page=i)
        page_queue.put(page_url)

    for x in range(3):
        th = Producer(page_queue,image_queue,name='生产者%s'%x)
        th.start()

    for x in range(5):
        th = Consumer(image_queue,name='消费者%s'%x)
        th.start()


if __name__ == '__main__':
    main()
